// pages/authorization/authorization.js
const {
  ScanQRcode,
  Qrcodelength
} = require('../../common/js/utils')
let conf = require('../../app.config').app
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cdn: conf.cdnPath,
    act: '',
    open: true,
    scanTabHeight:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.act)
    this.setData({
      act: options.act,
    });
    this.getSystem();
  },
  getSystem(){
    let _this = this;
    wx.getSystemInfo({
      success: function(res) {
        var w = res.windowWidth;
        var h = res.windowHeight;
        var scanTabHeight = w * (1443/750);
        _this.setData({
          scanTabHeight:scanTabHeight,
        });
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  // 获取授权
  onGetUserInfo(res) {
    let that = this

   wx.requestSubscribeMessage({
     tmplIds: ["wDXvI8mYGgLRDmj4zOef1Ub8XuR-Wx_NnB5wfFl1Ffo", "vUCAXpjImMVfwxPDJ-_0GUQsZut9nIWp5nA388ppsNc", "Qqoco1awk2ufenRNl6LeQzKmpkv1sP6vZgii2dLFksw"],
     success:res=>{
      that.api.interface.userSubscribeMessage({type: 2});
      that.api.interface.userSubscribeMessage({type: 4});
      that.api.interface.userSubscribeMessage({type: 5});
      that.api.login().then(res => {
        if (wx.getStorageSync('scancode')) {
          // 请求接口提交
          that.api.interface.getCustomerInfo().then(res => {
            wx.setStorageSync('userinfo', res.data.data)
          })
          that.api.interface.smallScanPoint({
            pinCode: wx.getStorageSync('scancode')
          }).then(result => {
            ScanQRcode(result)
          })
        } else if (that.data.act && that.data.act == 1) {
          wx.reLaunch({
            url: '/pages/home/home',
          })
        } else if (that.data.act && that.data.act == 2) {
          wx.reLaunch({
            url: '/pages/star/back',
          })
        } else if (that.data.act && that.data.act == 3) {
          // 送你1张优惠券 领取成功
          wx.reLaunch({
            url: '/pages/home/home?shareType=送你1张优惠券&shareGetType=授权完毕新用户领取成功&openid=' +  wx.getStorageSync('shareopenid')
          })
        }  else if (that.data.act && that.data.act == 4) {
          // 送你1份试用礼 领取成功
          wx.reLaunch({
            url: '/pages/home/home?shareType=送你1份试用礼&shareGetType=授权完毕新用户领取成功&friendOntrialOrderId=' +  wx.getStorageSync('friendOntrialOrderId')
          })
        }  else if (that.data.act && that.data.act == 5) {
          wx.reLaunch({
            url: '/pages/store/store'
          })
        }  else if (that.data.act && that.data.act == 6) {
          wx.reLaunch({
            url: '/pages/client/client'
          })
        } else {
          wx.navigateTo({
            url: '/pages/qna/result',
          })
        }
      })
     }
   })
  },
  // 返回上一页
  ongoback() {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})